<?php
namespace app\banquet\model;

use app\BaseModel;
use think\facade\Db;

class Evaluate extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_banquet_evaluate';


    protected $append = [


        'time_text'

    ];


    /**
     * @author chenniang
     * @DataTime: 2022-01-15 10:52
     * @功能说明:转换图片格式
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);
        }

    }

    /**
     * @author chenniang
     * @DataTime: 2022-01-11 15:17
     * @功能说明:友好时间
     */
    public function getTimeTextAttr($value,$data){

        if(!empty($data['create_time'])){

            return lb_friendly_date($data['create_time']);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->where('status','>',-1)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-11 14:54
     * @功能说明:用户评价列表 3条
     */
    public function userEvaListLimit($place_id,$limit=3){

        $dis = [

            'a.place_id' => $place_id,

            'a.status'   => 1
        ];

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->where($dis)
                ->field('a.*,b.nickName,b.avatarUrl')
                ->group('a.id')
                ->order('a.id desc')
                ->limit($limit)
                ->select()
                ->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-11 14:54
     * @功能说明:用户评价列表
     */
    public function userEvaList($dis,$page=10){

        $data = $this->alias('a')
            ->join('longbing_card_user b','a.user_id = b.id','left')
            ->where($dis)
            ->field('a.*,b.nickName,b.avatarUrl')
            ->group('a.id')
            ->order('a.id desc')
            ->paginate($page)
            ->toArray();

        return $data;

    }


    /**
     * @param $place_id
     * @功能说明:修改场地的评分
     * @author chenniang
     * @DataTime: 2022-01-11 15:49
     */
    public function updatePlaceStar($place_id){

        $dis[] = ['place_id','=',$place_id];

        $dis[] = ['status','>',-1];

        $hotel_star   = $this->where($dis)->avg('hotel_star');

        $sale_star    = $this->where($dis)->avg('sale_star');

        $place_star   = $this->where($dis)->avg('place_star');

        $average_star = $this->where($dis)->avg('average_star');

        $update = [

            'hotel_star'   => $hotel_star,

            'sale_star'    => $sale_star,

            'place_star'   => $place_star,

            'average_star' => $average_star,
        ];

        $place_model = new Place();

        $res = $place_model->dataUpdate(['id'=>$place_id],$update);

        return $res;

    }






}